// pages/posts/post-detail/post-detail.js
var postData = require('../../../data/posts-data.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    postId: '',
    isPlaying: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 文章编号的本地数据化
    var postId = options.id
    this.setData({
      postId: postId
    })
    // 获取文章数据，设置到本地 data 中
    var postDetail = postData.postList[postId]
    this.setData({
      postDetail: postDetail
    })
    // 收藏状态与收藏标志的设定
    var postsCollected = wx.getStorageSync('posts_Collected')
    var postCollected = postsCollected[postId]
    if (!postsCollected) {
      var postsCollected = {}
    }
    if (postCollected) {
      this.setData({
        collected: postCollected
      })
    } else {
      postsCollected[postId] = false
      wx.setStorageSync('posts_Collected', postsCollected)
    }
  },
  onCollectTap: function(event) {
    var postsCollected = wx.getStorageSync('posts_Collected')
    var currentCollect = postsCollected[this.data.postId]

    // if (currentCollect != true){
    //   currentCollect = true
    //   postsCollected[this.data.postId] = currentCollect
    // }else {
    //   currentCollect = false
    //   postsCollected[this.data.postId] = currentCollect
    // }
    // 收藏与否的设定
    currentCollect = !currentCollect
    // 更新本地化缓存中收藏的状态
    postsCollected[this.data.postId] = currentCollect

    // wx.showToast({
    //   title: currentCollect ? '收藏成功' : '取消成功',
    //   duration: 1000,
    //   icon: 'loading'
    // })
    this.showModal(postsCollected, currentCollect)
  },
  showModal: function(postsCollected, currentCollect) {
    var that = this
    wx.showModal({
      title: '收藏文章',
      content: currentCollect ? '收藏该文章？' : '取消收藏该文章？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#333',
      confirmText: '确认',
      confirmColor: '#405f80',
      success: function(res) {
        if (res.confirm) {
          // 更新缓存中收藏的状态
          wx.setStorageSync('posts_Collected', postsCollected)
          // 更新当前文章的收藏状态
          that.setData({
            collected: currentCollect
          })
        }
      }
    })
  },
  onShareTap: function(event){
    wx.showActionSheet({
      itemList: ['分享给微信好友', '分享到朋友圈', '分享到QQ', '分享到微博'],
      itemColor: '#405f80',
      success: function(res) {
        console.log(res.tapIndex)
      }
    })
  },
  onMusicTap(event){
    // console.log(event)
    // wx.getBackgroundAudioManager({
    //   dataUrl: 'http://ws.stream.qqmusic.qq.com/C100003507bR0gDKBm.m4a?fromtag=38',
    //   title: '夜夜夜夜-齐秦',
    //   coverImgUrl: 'http://y.gtimg.cn/music/photo_new/T002R150x150M000001TEc6V0kjpVC.jpg?max_age=2592000',
    //   success: function(res) {},
    //   fail: function(res) {},
    //   complete: function(res) {},
    // })
    var isPlaying = this.data.isPlaying
    const backgroundAudioManager = wx.getBackgroundAudioManager()
    backgroundAudioManager.title = '此时此刻'
    backgroundAudioManager.epname = '此时此刻'
    backgroundAudioManager.singer = '许巍'
    backgroundAudioManager.coverImgUrl = 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000'
    // 设置了 src 之后会自动播放
    backgroundAudioManager.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'
    if (isPlaying){
      // 暂停
      BackgroundAudioManager.pause()
      this.data.isPlaying = true
    }else{
      // 播放
      this.data.isPlaying = false
    }
  }

})